package com.bolt.report.engine.model;

import java.util.ArrayList;
import java.util.List;

public class Table {

    private List<Row> rows;

    private List<Column> columns;

    public Table() {
        this.rows = new ArrayList<>();
        this.columns = new ArrayList<>();
    }

    public Table(List<Row> rows, List<Column> columns) {
        this.rows = rows;
        this.columns = columns;
    }

    public Row getRow(int rowIndex) {
        if (rowIndex >= rows.size()) {
            rowIndex = rows.size() - 1;
            return rows.get(rowIndex);
        } else {
            return rows.get(rowIndex);
        }
    }

    public Column getColumn(int index) {
        if (index >= getColumns().size()) {
            index = getColumns().size() - 1;
        }
        return getColumns().get(index);
    }

    public List<Row> getRows() {
        return rows;
    }

    public List<Column> getColumns() {
        return columns;
    }


    public Cell getTopCell(int rowIndex, int cellIndex) {
        if (rowIndex <= 0 || cellIndex > columns.size() || rowIndex >= rows.size()) {
            return null;
        }
        return getRows().get(rowIndex - 1).getCell(cellIndex);
    }

    public Cell getLeftCell(int rowIndex, int cellIndex) {
        if (rowIndex <= 0 || cellIndex <= 0 || rowIndex >= (rows.size())) {
            return null;
        }
        return getRows().get(rowIndex).getCell(cellIndex - 1);
    }

    public Cell getRightCell(int rowIndex, int cellIndex) {

        if (rowIndex <= 0 || cellIndex <= 0 || cellIndex >= columns.size() || rowIndex >= rows.size()) {
            return null;
        }
        return getRows().get(rowIndex).getCell(cellIndex + 1);
    }

    public Cell getBottomCell(int rowIndex, int cellIndex) {
        if (rowIndex <= 0 || cellIndex <= 0 || rowIndex + 1 >= rows.size() || cellIndex > columns.size()) {
            return null;
        }
        return getRows().get(rowIndex + 1).getCell(cellIndex);
    }

}